package cn.hk.user.service.impl;

import cn.hk.common.entity.UserRelationConstant;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.domain.UserRelation;
import cn.hk.user.domain.vo.FamilyDetailVo;
import cn.hk.user.domain.vo.FamilyPersonVo;
import cn.hk.user.domain.vo.ChangeVo;
import cn.hk.user.mapper.UserInfoMapper;
import cn.hk.user.mapper.UserRelationMapper;
import cn.hk.user.service.UserFamilyService;
import cn.hk.user.service.UserInfoService;
import cn.hk.user.service.UserRelationService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 * 家庭关系表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-07
 */
@Service
public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, UserRelation> implements UserRelationService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserRelationMapper userRelationMapper;

    @Autowired
    private UserFamilyService userFamilyService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void addUserRelation(FamilyPersonVo familyPersonVo, UserInfo userInfo) {
        if (userRelationMapper.selectOneByToUserId(userInfo.getId())==null){
            UserRelation userRelation = new UserRelation();
            userRelation.setFormUserId(familyPersonVo.getFromUserId());
            userRelation.setToUserId(userInfo.getId());
            userRelation.setRealtion(familyPersonVo.getRelation());
            userRelationMapper.insertUserRelation(userRelation);
        }

    }

    @Override
    public List<FamilyDetailVo> familyMemberList(long userIdLong) {
        ArrayList<FamilyDetailVo> distinctResultList = null;
        try {
            List<FamilyDetailVo> resultList = CollUtil.list(false);
            List<FamilyDetailVo> familyDetailVosFromUserId = userRelationMapper.selectFamilyDetailVosByFromUserId(userIdLong);
            List<FamilyDetailVo> familyDetailVosToUserId = userRelationMapper.selectFamilyDetailVosByToUserId(userIdLong);
            resultList.addAll(familyDetailVosFromUserId);
            resultList.addAll(familyDetailVosToUserId);
            distinctResultList = CollUtil.distinct(resultList);
        } catch (Exception e) {
            Console.error("发生el异常",e);
        }

        return distinctResultList;
    }
    //关系转换
    private String RelationConvert(String realtion) {
        AtomicReference<String> resultValue = new AtomicReference<>("");
        UserRelationConstant userRelationConstant = new UserRelationConstant();
        Map<String, Object> beanToMap = BeanUtil.beanToMap(userRelationConstant);
        beanToMap.forEach((key, value) -> {
            if (realtion.equals(key)){
                resultValue.set(value+"");
            }
        });
        return resultValue.get();
    }

    @Override
    public AjaxResult removeFamilyMember(ChangeVo changeVo) {
        List<Long> fromUserIdList = userRelationMapper.selectByFromUserId(changeVo.getFromUserId());
        //被添加者无法删除添加者
        if (CollUtil.contains(fromUserIdList, changeVo.getToUserId())){
            return AjaxResult.error("该用户无法删除").put("msg", "该用户无法删除");
        }

        //家庭关系里面删除
        if(userRelationMapper.deleteUserRelationByUserId(changeVo.getFromUserId(), changeVo.getToUserId())){
            return AjaxResult.success("刪除成功");
        }else {
            return AjaxResult.error("刪除失败");
        }


    }

    @Override
    public AjaxResult editFamilyMemberRelation(ChangeVo changeVo) {
        //先判断是否存在对应家庭关系
        if(userRelationMapper.selectOneByFromUserIdAndToUserId(changeVo.getFromUserId(),changeVo.getToUserId())!=null){
            //存在则允许修改
            if(userRelationMapper.updateFamilyMemberRelation(changeVo.getFromUserId(), changeVo.getToUserId(), changeVo.getRelation())){
                AjaxResult.success().put("msg", "修改成功");
            }
        }
        //不存在则不允许修改
        return AjaxResult.error().put("msg","无法修改该用户关系");
    }


}
